package org.elephas.webapp.model;

import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.elephas.model.User;
import org.elephas.service.UserFinder;

public class DetachableUserModel extends LoadableDetachableModel<User> {
	private static final long serialVersionUID = 1L;

	@SpringBean
	private UserFinder service;

	private String emailAddress;

	public DetachableUserModel() {
		this(null);
	}
	
	public DetachableUserModel(User user) {
		if (user != null) {
			emailAddress = user.getEmailAddress();
		}
		InjectorHolder.getInjector().inject(this);

	}

	@Override
	protected User load() {
		if (emailAddress == null) {
			return new User();
		}
		return service.findByEmail(emailAddress);
	}

}
